﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Web.Core.Helper
{
    /// <summary>
    /// 区域与IP地址帮助类
    /// </summary>
    public class AreaHelper
    {
        const string cookieName = "area";
        static string _areaname = string.Empty;

        /// <summary>
        /// 得到当前用户所在区域
        /// </summary>
        public static string AreaName
        {
            get
            {

                if (string.IsNullOrEmpty(_areaname))
                {
                    _areaname = HttpUtility.UrlDecode(VCommons.Http.CookieHelper.Read(cookieName));
                    if (string.IsNullOrEmpty(_areaname))
                        _areaname = getIPForArea();
                }
                return _areaname;
            }
        }

        /// <summary>
        /// 设置区域
        /// </summary>
        /// <param name="areaname"></param>
        /// <returns></returns>
        public static bool SetArea(string areaname)
        {
            string[] aryOpenAreaName = "全国|北京市|安徽省".Split(new char[] { '|' });
            if (aryOpenAreaName.Contains(areaname))
            {
                //----编码
                string areaname_code = HttpUtility.UrlEncode(areaname);
                _areaname = areaname;
                VCommons.Http.CookieHelper.Write(cookieName, areaname_code, 60 * 24 * 365);//有效期一年 最小单位 1分钟
                return true;
            }
            return false;
        }

        /// <summary>
        /// 通过ip获取地域（异常为:北京市）
        /// </summary>
        /// <returns></returns>
        static string getIPForArea()
        {
            string ip = VCommons.IPHelper.getRealIPAddress;

            string areaname = "全国";
            if (!ip.Equals("127.0.0.1"))
            {
                try
                {
                    string strHtml = new VCommons.Http.WebClient().GetHtml("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
                    IPModel entity = Newtonsoft.Json.JsonConvert.DeserializeObject<IPModel>(strHtml);
                    if (entity.Code == 0
                           && !string.IsNullOrEmpty(entity.Data.city))
                    {
                        areaname = entity.Data.city;
                    }
                }
                catch { }
            }
            SetArea(areaname);
            return areaname;
        }
    }

    /// <summary>
    /// IP和区域模型
    /// </summary>
    public class IPModel
    {
        public int Code { get; set; }
        public Data Data { get; set; }

    }
    public class Data
    {
        public string country { get; set; }
        public string country_id { get; set; }
        public string area { get; set; }
        public string area_id { get; set; }
        public string region { get; set; }
        public string region_id { get; set; }
        public string city { get; set; }
        public string city_id { get; set; }
        public string county { get; set; }
        public string county_id { get; set; }
        public string isp { get; set; }
        public string isp_id { get; set; }
        public string ip { get; set; }
    }
}
